/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package se.sll.service.catalogue.importer.transform;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import lombok.extern.slf4j.Slf4j;
import org.apache.camel.Exchange;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import se.sll.service.catalogue.common.rest.model.Product;
import se.sll.service.catalogue.common.rest.model.ProductReference;
import se.sll.service.catalogue.common.rest.model.Unit;
import se.sll.service.catalogue.importer.extract.HsaIdVmIdProcessor;
import se.sll.service.catalogue.importer.extract.VMCareGiver;
import se.sll.service.catalogue.model.vm.Vantetid;
import se.sll.service.catalogue.model.vm.Vardgivare;

/**
 *
 * @author khaleddaham
 */
@Slf4j
public class ToOffers {

    @SuppressWarnings("unchecked")
    public Exchange process(Exchange exchange) throws JsonParseException, JsonMappingException, IOException {
        Map map = (Map) exchange.getIn().getBody();
        map.put("offer", getOfferCreateRequestList((List<Vantetid>) exchange.getProperty("capacities")));
        exchange.getIn().setBody(map);
        return exchange;
    }

    /**
     * Add our vardgivare via REST to offers
     */
    public static void addOffers(List<Vardgivare> cList, List<Vantetid> vList) throws UnsupportedEncodingException, FileNotFoundException, IOException, Exception {
        HsaIdVmIdProcessor p = new HsaIdVmIdProcessor();
        for (Vardgivare v : cList) {
            VMCareGiver vm = new VMCareGiver();
            String hsaId = p.getHSAIdForVMId(vm);
        }
    }

    /**
     *
     * @param vardGivare
     * @return
     * @throws FileNotFoundException
     * @throws IOException
     */
    private Set<Unit> getOfferCreateRequestList(List<Vantetid> vList) throws FileNotFoundException, IOException {
        Set<Unit> obj = new HashSet<Unit>();
        if (vList != null) {
            HsaIdVmIdProcessor p = new HsaIdVmIdProcessor();
            for (Vantetid v : vList) {
                VMCareGiver vm = new VMCareGiver();
                vm.setId(v.getVardgivar().getVardgivarID());
                vm.setPartId(v.getVardgivarDel().getVardgivardelID());
                String hsaId = p.getHSAIdForVMId(vm);
                Unit u = new Unit();
                u.setRegionalUnitId(hsaId);
                u.setUnitId(hsaId);
                // HSA Cache
                u.setRelativeDistinguishedName("");
                u.setProductReferences(new ArrayList<ProductReference>());
                // Add prodref
                for (Vantetid v2 : vList) {
                    VMCareGiver vm2 = new VMCareGiver();
                    vm2.setId(v2.getVardgivar().getVardgivarID());
                    vm2.setPartId(v2.getVardgivarDel().getVardgivardelID());
                    if(vm.equals(vm2)) {
                        ProductReference pr = new ProductReference();
                        Product product = new Product();
                        product.setName(v2.getAtgard().getAtgardID());
                        pr.setComment(v2.getFritext());
                        pr.setProduct(product);
                        u.getProductReferences().add(pr);
                    }                    
                }
                obj.add(u);
            }
        } else {
            log.error("No vardGivare found.");
        }
        return obj;
    }
}
